Skip to content

sched: split tcb->flags by writer - #22238

Closed
zyfeier wants to merge 3 commits into
apache:masterfrom
zyfeier:tcb-flags-split
Closed

zyfeier wants to merge 3 commits into
apache:masterfrom
zyfeier:tcb-flags-split

Conversation

@zyfeier

@zyfeier zyfeier commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Why. Five bits of tcb->flags are changed only by the task itself: TCB_FLAG_SYSCALL, TCB_FLAG_SIGNAL_ACTION, TCB_FLAG_SIGMASK_SAVED, TCB_FLAG_PREEMPT_SCHED and TCB_FLAG_UNINTERRUPTIBLE. They are also the only bits that the system call, signal delivery, semaphore wait, sched_unlock() and timer tick paths change or test. The rest of the word is written from other CPUs as well: nxsched_suspend(), nxsig_queue_action() and nxtask_restart() set TCB_FLAG_CPU_LOCKED on a task that is running elsewhere. As long as both groups share one word, a plain read-modify-write on either side can lose the other side's update, and making the word atomic puts atomic operations on every one of those hot paths.

What. Move the self-written bits to a byte of their own:

  • tcb->exec_flags (uint8_t): TCB_EXEC_SYSCALL, TCB_EXEC_SIGNAL_ACTION, TCB_EXEC_SIGMASK_SAVED, TCB_EXEC_PREEMPT_SCHED, TCB_EXEC_UNINTERRUPTIBLE. Only the task itself changes them, so plain loads and stores are enough. The thread type (TCB_FLAG_TTYPE_*) moves here too: the creator of the TCB sets it before the task runs and it never changes, so it is read with a plain load.
  • tcb->sched_policy (uint8_t): holds SCHED_FIFO, SCHED_RR, SCHED_SPORADIC or SCHED_OTHER directly. Other tasks change it, but always as a whole value.

The two bytes fill the 2-byte hole after init_priority, so struct tcb_s keeps its size.

A plain read-modify-write of exec_flags cannot lose an update:

  • no other task and no interrupt handler writes it: sched_unlock() returns early in interrupt context, and a system call is raised by the task itself;
  • a signal action that runs between the load and the store of such a change restores every bit it sets before it returns;
  • a byte of its own is a separate memory location, so writing it cannot disturb an update of tcb->flags on another CPU.

With this change a cross-CPU write of TCB_FLAG_CPU_LOCKED can no longer clobber the hot-path bits. The bits left in tcb->flags are made atomic in #21804, which depends on this PR.

How.

  1. sched, arch: fix nxstyle errors in files touched by the tcb flags change: whitespace only, in files that this PR and sched: split tcb->flags by writer #21804 change.
  2. sched: move the per-task hot-path flags out of tcb->flags: adds exec_flags and sched_policy and converts their users. Removes TCB_FLAG_SYSCALL, TCB_FLAG_SIGNAL_ACTION, TCB_FLAG_SIGMASK_SAVED, TCB_FLAG_PREEMPT_SCHED, TCB_FLAG_UNINTERRUPTIBLE, TCB_FLAG_POLICY_* and TCB_FLAG_SCHED_*, so that out-of-tree code still using them fails to build instead of reading the wrong bits.
  3. sched: move the thread type into tcb->exec_flags: the type moves to bits 0-1 of exec_flags. TCB_FLAG_TTYPE_MASK becomes TCB_EXEC_TTYPE_MASK, so that code still reading the type from tcb->flags fails to build; the TCB_FLAG_TTYPE_TASK/PTHREAD/KERNEL values keep their names and values, since they are also passed as the ttype argument of up_create_stack() and friends.

Impact

  • Users / API: none visible to applications. struct tcb_s is kernel-internal; out-of-tree code that uses the removed TCB_FLAG_* macros must switch to exec_flags / sched_policy, and read the thread type as tcb->exec_flags & TCB_EXEC_TTYPE_MASK. tools/pynuttx/nxgdb/thread.py reads sched_policy.
  • Build: none.
  • Hardware: none. sizeof(struct tcb_s) is unchanged (checked with ptype /o on stm32l0538-disco:nsh 184, mps3-an547:knsh 248, qemu-armv7a:knsh_smp 272, qemu-armv8a:nsh_smp 360, rv-virt:ksmp64 416, sim:ostest 416 bytes).
  • Documentation: Documentation/guides/concurrency/fork_vfork_migration.rst and Documentation/os/scheduling/index.rst use the new names.
  • Security: none.
  • Compatibility: see Users / API.

Testing

Host: Ubuntu x86_64, CI image ghcr.io/apache/nuttx/apache-nuttx-ci-linux, master c32466a, apps 8ff87439bf4 (clean worktree) for the build. The runtime results below were measured on master 960459c: on the first two commits of this PR, and again on the whole series of #21804, which includes the third commit. They will be refreshed on the current base.

Build (make EXTRAFLAGS="-Wno-cpp -Werror"): all 20 pass: stm32l0538-disco:nsh, stm32f4discovery:nsh, stm32f4discovery:sporadic, stm32l476vg-disco:knsh, nucleo-g431rb:nsh, nucleo-g431rb:cansock, mps2-an521:nsh, mps3-an547:knsh, raspberrypi-pico-2:smp, qemu-armv7a:nsh, qemu-armv7a:knsh_smp, qemu-armv8a:nsh, qemu-armv8a:knsh, qemu-armv8a:nsh_smp, fvp-armv8r:pnsh_smp, rv-virt:knsh, rv-virt:ksmp64, sim:ostest, esp32-devkitc:nsh, verdin-mx8mp:rpmsg. tools/checkpatch.sh -c -u -m -g passes on every commit.

Runtime, ostest user_main: Exiting and ostest_main: Exiting with status 0, with no fault in the log:

Configuration Covers
qemu-armv8a:nsh_smp_tickless (4 CPUs) PREEMPT_SCHED in sched_unlock()
qemu-armv8a:citest_smp (4 CPUs) the configuration CI runs
qemu-armv8a:knsh arm64 kernel build, system call path
qemu-armv7a:knsh armv7-a kernel build, system call path
sim:ostest full run

qemu-intel64:citest_smp (4 CPUs), LTP open POSIX signal tests: 30 pass: kill_1_1, kill_2_1, killpg_1_1, killpg_2_1, sigaction_1_1, sigwait_1_1, sigpending_1_1, pthread_kill_1_1, sigaddset_1_1, raise_1_1, sigprocmask 5_1 6_1 7_1 8_1 8_2 8_3 9_1 10_1 12_1 15_1 17_1, sigqueue 2_1 2_2 4_1 5_1 6_1 7_1 8_1 10_1 11_1. sigaltstack_1_1 is unresolved (Error 22) because sigaltstack() is not implemented.

The next change converts the tcb->flags users in these files.  They
carry style errors that nxstyle reports for any change to the file:
missing blank lines after declarations, a mis-indented DEBUGASSERT in
irq_dispatch.c and mis-indented switch cases in minerva_swint.c.  Fix
them first, so that the conversion itself stays easy to review.

No functional change.

Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
Five bits of tcb->flags are changed only by the task itself: SYSCALL,
SIGNAL_ACTION, SIGMASK_SAVED, PREEMPT_SCHED and UNINTERRUPTIBLE.  The
scheduling policy is written by other tasks, but always as a whole value.
These are also the only bits that the system call, signal delivery,
semaphore wait, sched_unlock() and timer tick paths change or test.

Move the five bits into a new byte, tcb->exec_flags, and the policy into
tcb->sched_policy, which holds SCHED_FIFO, SCHED_RR, SCHED_SPORADIC or
SCHED_OTHER directly.  Both bytes fill the existing 2-byte hole after
init_priority, so struct tcb_s keeps its size on 32-bit and 64-bit
targets.

Since no other task and no interrupt handler writes exec_flags, a plain
read-modify-write of it cannot lose an update; a signal action that runs
in the middle of such a write restores every bit it sets before it
returns.  A byte of its own is a separate memory location, so changing
exec_flags or sched_policy cannot disturb a concurrent atomic update of
tcb->flags on another CPU.

This leaves the remaining tcb->flags bits, which are written from more
than one context, to be made atomic without adding atomic operations to
the paths above.

The TCB_FLAG_SYSCALL, TCB_FLAG_SIGNAL_ACTION, TCB_FLAG_SIGMASK_SAVED,
TCB_FLAG_PREEMPT_SCHED, TCB_FLAG_UNINTERRUPTIBLE and TCB_FLAG_POLICY_*
/ TCB_FLAG_SCHED_* definitions are removed, so out-of-tree code that
still uses them fails to build instead of reading the wrong bits.

Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
The next commit makes the bits of tcb->flags atomic, because other CPUs
change some of them.  The thread type does not need that.  The creator
of the TCB sets it before the task runs, and it never changes after
that.  It is kept in tcb->flags only because the bits used to be packed
together, but it is read on many paths: task creation and exit, group
and signal handling, fault handling and the file systems.

Move the type to bits 0-1 of tcb->exec_flags and the TCB_EXEC_* bits to
bits 2-6.  Nothing else writes exec_flags while the creator sets the
type: the task does not run yet and no other task writes exec_flags.
After that, the task's own updates of exec_flags write the unchanged
type back, so the type can be read with a plain load on any CPU.

TCB_FLAG_TTYPE_MASK is renamed TCB_EXEC_TTYPE_MASK, so that code which
still reads the type from tcb->flags fails to build.  The type values
keep their names and values: they are also passed as the ttype argument
of up_create_stack() and friends and match CHILD_FLAG_TTYPE_*.

Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>

#ifdef CONFIG_BUILD_KERNEL
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
if ((tcb->exec_flags & TCB_EXEC_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep exec_flags and macro name as before to reduce the chanage

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: ceva Issues related to CEVA architecture Arch: hc Issues related to HC architecture Arch: mips Issues related to the MIPS architecture Arch: openrisc Issues related to the OpenRISC architecture Arch: renesas Issues related to the Renesas chips labels Nov 24, 2026
@github-actions github-actions Bot added Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: tricore Issues related to the TriCore architecture from Infineon Arch: x86 Issues related to the x86 architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Arch: z16 Issues related to the Z16 architecture Arch: z80 Issues related to the Z80 architecture Size: L The size of the change in this PR is large Area: BINFMT Arch: rh850 labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +6 B (+0.0%, 64,455 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text -80 B (-0.1%, 133,320 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text -80 B (-0.1%, 98,976 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text -4 B (-0.0%, 86,360 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -4 B (-0.0%, 66,252 B / 131,072 B, total: 51% used)

mps2-an500-citest

  • flash: .text +72 B (+0.0%, 247,352 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +80 B (+0.1%, 116,552 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.dump_task +4 B, .text.group_cancel_children_handler +4 B, .text.nx_pthread_create -12 B, .text.nx_start +4 B, .text.nxsched_get_scheduler -4 B, .text.nxsched_process_timer -4 B, .text.nxsched_set_scheduler -4 B, .text.nxtask_setup_fork -8 B, .text.nxthread_create +4 B, .text.nxthread_setup_scheduler -4 B, .text.proc_read -4 B, .text.pthread_cancel +4 B, .text.sched_unlock -4 B, .text.task_spawn +4 B (-0.0%, 377,444 B)

qemu-intel64

  • Code: .text -265 B (-0.0%, 8,706,626 B)
  • Data: .rodata +64 B (+0.1%, 109,121 B)

rx65n-rsk2mb

  • ROM: .text +32 B (+0.0%, 92,780 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text -16 B (-0.0%, 408,400 B)

stm32-nucleo-f103rb

  • flash: .text -12 B (-0.0%, 35,088 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@zyfeier zyfeier closed this Nov 24, 2026
@zyfeier
zyfeier deleted the tcb-flags-split branch November 24, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: ceva Issues related to CEVA architecture Arch: hc Issues related to HC architecture Arch: mips Issues related to the MIPS architecture Arch: openrisc Issues related to the OpenRISC architecture Arch: renesas Issues related to the Renesas chips Arch: rh850 Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: tricore Issues related to the TriCore architecture from Infineon Arch: x86 Issues related to the x86 architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Arch: z16 Issues related to the Z16 architecture Arch: z80 Issues related to the Z80 architecture Area: BINFMT Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants